#import "ContactsView.h"

@implementation ContactsView

#pragma mark Interface methods

- (void)viewDidLoad {
    [super viewDidLoad];
	[self.navigationItem setTitle:@"Contacts"];
	UIBarButtonItem * editButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(editContacts)];
	[self.navigationItem setRightBarButtonItem:editButton];
	[editButton release];
	[self.parentViewController.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"StripeBackground.png"]]];	
		
	contactsTableView = [[ShadowedTableView alloc] init];
	[contactsTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
	[contactsTableView setBackgroundColor:[UIColor clearColor]];
	[contactsTableView setDelegate:self];
	[contactsTableView setDataSource:self];
	[self setView:contactsTableView];
	
	editingContacts = NO;
}

- (void)editContacts {
	if (!editingContacts) {
		UIBarButtonItem * doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(editContacts)];
		[self.navigationItem setRightBarButtonItem:doneButton];
		[doneButton release];
		editingContacts = YES;
		
		UIBarButtonItem * addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
		[self.navigationItem setLeftBarButtonItem:addButton];
		[addButton release];
		
		[contactsTableView setEditing:YES animated:YES];
	} else {
		UIBarButtonItem * editButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(editContacts)];
		[self.navigationItem setRightBarButtonItem:editButton];
		[self.navigationItem setLeftBarButtonItem:nil];
		[editButton release];
		editingContacts = NO;
		
		[contactsTableView setEditing:NO animated:YES];
	}
}

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 50;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	return @"Online (4/7)";
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[ClearLabelCellView alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
		[cell setBackgroundView:[[[GradientView alloc] init] autorelease]];
		[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
    }
    
    [cell.textLabel setText:@"Joost"];
	[cell.textLabel setShadowOffset:CGSizeMake(0, 1)];
	[cell.textLabel setShadowColor:[UIColor whiteColor]];
	
	[cell.detailTextLabel setText:@"Trying out Waim"];
	[cell.detailTextLabel setTextColor:[UIColor grayColor]];
	[cell.detailTextLabel setShadowOffset:CGSizeMake(0, 1)];
	[cell.detailTextLabel setShadowColor:[UIColor whiteColor]];
	
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        
    }   
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
	return @"Delete Contact";
}

#pragma mark Memory methods

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc {
	[applicationView release];
	[contactsTableView release];
    [super dealloc];
}

@end
